//
//  ShopNavBarView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/6/23.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "ShopNavBarView.h"
#import "UIView+Extension.h"
#import "MTShopSingleModel.h"
#import <YBPopupMenu.h>

#define NAVBAR_CHANGE_POINT 50

@interface ShopNavBarView()<YBPopupMenuDelegate>

proStrong UIButton *backBtn;

proStrong UIView *searchView;
proStrong UILabel *searchLabel;

@end

@implementation ShopNavBarView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
    }
    return self;
}

- (void)addViews {
    
    self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];//透明度

    //返回按钮
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backBtn.frame = CGRectMake(0, 20+kDefaultNavBar_SubView_MinY, 54, 44);
    [self.backBtn setImage:[UIImage imageNamed:@"icon_back_white"] forState:UIControlStateNormal];
    [self.backBtn setImage:[UIImage imageNamed:@"icon_back_white"] forState:UIControlStateHighlighted];
    self.backBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [self.backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0 ,0)];
    [self.backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, 0)];
    [self.backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.backBtn];
    
    //搜索框
    self.searchView = [[UIView alloc]initWithFrame:CGRectMake(self.width-37*4-10, 20+kDefaultNavBar_SubView_MinY+ 5 + 5, 37, 24)];
    self.searchView.backgroundColor = kColorBgColor;
    [self addSubview:self.searchView];
    
    self.searchLabel = [Utils genLabelWithFrame:CGRectMake(30, 2, 120, 20) textAlign:NSTextAlignmentLeft fontSize:12 textColor:kColorGray];
    self.searchLabel.text = @"请输入商品名称";
    [self.searchView addSubview:self.searchLabel];
    
    //搜索 信息  收藏 更多
    NSArray *imgAray = @[@"sousuo_white",@"collect_white",@"more_white"];
    for (int i = 0; i<3; i++) {
        UIButton *itemBT  = [UIButton buttonWithType:UIButtonTypeCustom];
        itemBT.frame = CGRectMake(self.width-37*(3-i)-5, 20+kDefaultNavBar_SubView_MinY+5, 37, 34);
        [itemBT setImage:[UIImage imageNamed:imgAray[i]] forState:UIControlStateNormal];
        [itemBT addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        itemBT.tag = i + 1;
        [self addSubview:itemBT];
    }
}

- (void)backAction {
    
    if (self.navBarViewBackAction) {
        self.navBarViewBackAction();
    }
}

- (void)btnClick:(UIButton *)sender {
    
    switch (sender.tag) {
        case 1: {
            break;
        }
        case 2: {
            break;
        }
        case 3: {
            [YBPopupMenu showRelyOnView:sender titles:@[@"我的购物车", @"消息中心"] icons:@[@"icon-shoppingcart",@"icon-messagecenter"] menuWidth:100 otherSettings:^(YBPopupMenu *popupMenu) {
                popupMenu.cornerRadius = 2;
                popupMenu.fontSize = 12;
                popupMenu.textColor = kColorGray;
                popupMenu.arrowWidth = 10;
                popupMenu.arrowHeight = 7;
                popupMenu.itemHeight = 34;
                popupMenu.delegate = self;
                popupMenu.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            }];
            break;
        }
        default:
            break;
    }
}

/// 根据主滚动视图的Y偏移量刷新视图
/// @param offsetY Y偏移量
- (void)refreshWithOffsetY:(CGFloat)offsetY {
    
    float maxOffsetY = [MTShopSingleModel sharedInstance].maxOffsetY;
    float startChangeY = [MTShopSingleModel sharedInstance].startChangeY;
    
    float alpha = offsetY/maxOffsetY;
    if (alpha > 1) {
        alpha = 1;
    }
    if (alpha < 0) {
        alpha = 0;
    }
    
    CGFloat alpha2 = 0;
    if (alpha >= 0.6) {
        alpha2 = (alpha - 0.6)/0.4;
    }
    
    if (offsetY > NAVBAR_CHANGE_POINT) {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:alpha];
    } else {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    }
    UIButton *sousuBT = (UIButton *)[self viewWithTag:1];
    UIButton *collectBT = (UIButton *)[self viewWithTag:2];
    UIButton *messageBT = (UIButton *)[self viewWithTag:3];
    
    CGFloat startLoc = self.width-37*3-5;
    CGFloat endLoc = self.backBtn.right;
    CGFloat distance =  startLoc  -  endLoc;
    if (offsetY <= maxOffsetY && offsetY >= 0) {
        //18x18
        if (offsetY >= startChangeY) {
            CGFloat imgRate = (offsetY - startChangeY)/(maxOffsetY - startChangeY);
            if (imgRate >= 1) {
                imgRate = 1;
            }
            sousuBT.frame = CGRectMake(startLoc - imgRate*distance, 20+kDefaultNavBar_SubView_MinY+5, 37, 34);
            CGFloat imgRate2 = imgRate*3.5;
            if (imgRate2 >= 1) {
                imgRate2 = 1;
            }
            if (imgRate2  >= 1) {
                [sousuBT setImage:[UIImage imageNamed:@"icon-search box"] forState:UIControlStateNormal];
            }else{
                [sousuBT setImage:[UIImage imageNamed:@"sousuo_white"] forState:UIControlStateNormal];
            }
            sousuBT.imageEdgeInsets = UIEdgeInsetsMake(8*imgRate2, 8*imgRate2, 8*imgRate2, 8*imgRate2);
            self.searchView.x = sousuBT.x;
            self.searchLabel.x = 30;
            _searchView.backgroundColor = [kColorBgColor colorWithAlphaComponent:alpha2];
            if (alpha >= 0.8) {
                self.searchLabel.textColor = [kColorGray colorWithAlphaComponent:alpha];
            }else{
                self.searchLabel.textColor = [kColorGray colorWithAlphaComponent:0];
            }
            CGFloat sousuoBgRate = (offsetY - startChangeY)/(maxOffsetY- startChangeY);
            if (sousuoBgRate >= 1) {
                sousuoBgRate = 1;
            }
            self.searchView.width = distance*sousuoBgRate + 37;
        }
    }else{
        sousuBT.frame = CGRectMake(startLoc, 20+kDefaultNavBar_SubView_MinY+5, 37, 34);
        [sousuBT setImage:[UIImage imageNamed:@"sousuo_white"] forState:UIControlStateNormal];
        sousuBT.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        self.searchView.backgroundColor = [kColorBgColor colorWithAlphaComponent:0];
        self.searchView.x = sousuBT.x;
        self.searchView.width =  37;
        self.searchLabel.x = 30;
        self.searchLabel.textColor = [kColorGray colorWithAlphaComponent:0];
    }
    
    if (alpha >= 0.5) {
        [self.backBtn setImage:[UIImage imageNamed:@"back_grey"]    forState:UIControlStateNormal];
        [messageBT setImage:[UIImage imageNamed:@"more_grey"] forState:UIControlStateNormal];
        [collectBT setImage:[UIImage imageNamed:@"collect_grey"] forState:UIControlStateNormal];
    }else{
        [self.backBtn setImage:[UIImage imageNamed:@"icon_back_white"] forState:UIControlStateNormal];
        [messageBT setImage:[UIImage imageNamed:@"more_white"] forState:UIControlStateNormal];
        [collectBT setImage:[UIImage imageNamed:@"collect_white"]  forState:UIControlStateNormal];
    }
}

/// 恢复原始状态
- (void)recovery {
    
    UIButton *sousuBT = (UIButton *)[self viewWithTag:1];
    UIButton *collectBT = (UIButton *)[self viewWithTag:2];
    UIButton *moreBT = (UIButton *)[self viewWithTag:3];
    CGFloat startLoc = self.width-37*3-5;
    [UIView animateWithDuration:0.1 animations:^{
        self.searchLabel.textColor = [kColorGray colorWithAlphaComponent:0];
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [[UIColor whiteColor]  colorWithAlphaComponent:0];
        sousuBT.frame = CGRectMake(startLoc, 20+kDefaultNavBar_SubView_MinY+5, 37, 34);
        sousuBT.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        self.searchView.x = sousuBT.x;
        self.searchView.x = 30;
        self.searchView.backgroundColor = [kColorBgColor colorWithAlphaComponent:0];
        self.searchView.width =  37;
        [sousuBT   setImage:[UIImage imageNamed:@"sousuo_white"] forState:UIControlStateNormal];
        [moreBT setImage:[UIImage imageNamed:@"more_white"]   forState:UIControlStateNormal];
        [collectBT setImage:[UIImage imageNamed:@"collect_white"] forState:UIControlStateNormal];
    } completion:^(BOOL finished) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }];
}

#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index {
    
    //推荐回调
    if (index == 0) {
        //购物车
        NSLog(@"购物车");
    }else{
        //消息中心
        NSLog(@"消息中心");
    }
}

@end
